<?php
/**
 * licenses : MIT
 * creatTime: 2020/5/17
 * author : weblinuxgame
 * filename: UserProfileRepository.php
 */

namespace App\Repositories;

use App\Core\Utils\Result;
use App\Core\Utils\Message;
use App\Services\UserService;
use App\Core\Interfaces\ResultInterface;
use Illuminate\Support\Arr;

/**
 * Class UserProfileRepository
 * @package App\Repositories
 */
class UserProfileRepository
{
    protected $attributes = [
        'nickname', 'avatar_id',
    ];

    /**
     * 更新用户密码
     * @param int $id
     * @param string $password
     * @return ResultInterface
     */
    public function password(int $id, string $password): ResultInterface
    {
        if (empty($password) || empty($id)) {
            return Result::fail(Message::message(Message::MissRequiredParam), Message::MissRequiredParam);
        }
        if (UserService::getInstance()->updatePassword($id, $password)) {
            return Result::success(null);
        }
        return Result::fail(Message::message(Message::UpdateFailed), Message::UpdateFailed);
    }

    /**
     * 更新用户信息
     * @param int $id
     * @param array $attributes
     * @return ResultInterface
     */
    public function update(int $id, array $attributes = []): ResultInterface
    {
        if (empty($attributes) || empty($id)) {
            return Result::fail(Message::message(Message::MissRequiredParam), Message::MissRequiredParam);
        }
        $data = [];
        foreach ($this->attributes as $key) {
            $value = Arr::get($attributes, $key);
            if (empty($value)) {
                continue;
            }
            $data[$key] = $value;
        }
        if (empty($data)) {
            return Result::fail(Message::message(Message::MissRequiredParam), Message::MissRequiredParam);
        }
        if (UserService::getInstance()->setProfiles($id, $attributes)) {
            return Result::success(UserService::getInstance()->getById($id));
        }
        return Result::fail(Message::message(Message::UpdateFailed), Message::UpdateFailed);
    }

    /**
     * 绑定手机｜更新手机好
     * @param int $id
     * @param string $mobile
     * @return ResultInterface
     */
    public function mobile(int $id, string $mobile): ResultInterface
    {
        if (empty($attributes) || empty($id)) {
            return Result::fail(Message::message(Message::MissRequiredParam), Message::MissRequiredParam);
        }
        if (UserService::getInstance()->exists('mobile', $mobile)) {
            return Result::fail($mobile . Message::message(Message::RecordExists), Message::RecordExists);
        }
        if (UserService::getInstance()->setProfiles($id, ['mobile' => $mobile])) {
            return Result::success(UserService::getInstance()->getById($id));
        }
        return Result::fail(Message::message(Message::UpdateFailed), Message::UpdateFailed);
    }

    /**
     * 更新email
     * @param int $id
     * @param string $email
     * @return ResultInterface
     */
    public function email(int $id, string $email): ResultInterface
    {
        if (empty($attributes) || empty($id)) {
            return Result::fail(Message::message(Message::MissRequiredParam), Message::MissRequiredParam);
        }
        if (UserService::getInstance()->exists('email', $email)) {
            return Result::fail($email . Message::message(Message::RecordExists), Message::RecordExists);
        }
        if (UserService::getInstance()->setProfiles($id, ['email' => $email])) {
            return Result::success(UserService::getInstance()->getById($id));
        }
        return Result::fail(Message::message(Message::UpdateFailed), Message::UpdateFailed);
    }

    /**
     * 获取属性值
     * @param int $id
     * @param array $attributeKeys
     * @return ResultInterface
     */
    public function profiles(int $id, array $attributeKeys = ['*','!password', '!deleted_at']): ResultInterface
    {
        $data = UserService::getInstance()->getProfiles($id, $attributeKeys);
        if (!empty($data)) {
            return Result::success($data);
        }
        return Result::fail(Message::message(Message::UserProfileNotExists), Message::UserProfileNotExists);
    }
}
